#include "SDLSprite.h"

SDLSprite::SDLSprite(std::string _fileName)
	: SDLComponent(), m_PosX(0), m_PosY(0), m_FrameX(0), m_FrameY(0),m_Visible(true)
{
	if((m_Source = SDLResources::GetInstance()->GetTexture(_fileName)) == 0)
	{
		std::cout << "Cannot load image at path: \"" << _fileName << "\"" << std::endl;
	}
	else
	{
		m_Height = m_Source->h;
		m_Width = m_Source->w;
	}
}

SDLSprite::SDLSprite(int _frameX, int _frameY, int _width, int _height, std::string _fileName)
	: SDLComponent(), m_PosX(0), m_PosY(0), m_Height(_height), m_Width(_width), m_FrameX(_frameX), m_FrameY(_frameY), m_Visible(true)
{
	if((m_Source = SDLResources::GetInstance()->GetTexture(_fileName)) == 0)
	{
		std::cout << "Cannot load image at path: \"" << _fileName << "\"" << std::endl;
	}
}

SDLSprite::SDLSprite(int _posX, int _posY, int _frameX, int _frameY, int _width, int _height, std::string _fileName)
	: SDLComponent(), m_PosX(_posX), m_PosY(_posY), m_Height(_height), m_Width(_width), m_FrameX(_frameX), m_FrameY(_frameY), m_Visible(true)
{
	if((m_Source = SDLResources::GetInstance()->GetTexture(_fileName)) == 0)
	{
		std::cout << "Cannot load image at path: \"" << _fileName << "\"" << std::endl;
	}
}

SDLSprite::SDLSprite(int _posX, int _posY, int _frameX, int _frameY, int _width, int _height, std::string _fileName, SDLScene* _scene)
	: SDLComponent(_scene), m_PosX(_posX), m_PosY(_posY), m_Height(_height), m_Width(_width), m_FrameX(_frameX), m_FrameY(_frameY), m_Visible(true)
{
	if((m_Source = SDLResources::GetInstance()->GetTexture(_fileName)) == 0)
	{
		std::cout << "Cannot load image at path: \"" << _fileName << "\"" << std::endl;
	}
}

SDLSprite::SDLSprite(SDL_Surface* _surface)
	: SDLComponent(), m_Source(_surface), m_PosX(0), m_PosY(0), m_FrameX(0), m_FrameY(0), m_Visible(true)
{

}

SDLSprite::SDLSprite(SDL_Surface* _surface, SDLScene* _scene)
	: SDLComponent(_scene), m_Source(_surface), m_PosX(0), m_PosY(0), m_FrameX(0), m_FrameY(0), m_Visible(true)
{

}

SDLSprite::SDLSprite(int _posX, int _posY, SDL_Surface* _surface)
	: SDLComponent(), m_Source(_surface), m_PosX(_posX), m_PosY(_posY), m_FrameX(0), m_FrameY(0), m_Visible(true)
{

}

SDLSprite::SDLSprite(const SDLSprite& _source)
	: m_Source(_source.m_Source), m_PosX(_source.m_PosX), m_PosY(_source.m_PosY), m_FrameX(_source.m_FrameX), m_FrameY(_source.m_FrameY), m_Width(_source.m_Width), m_Height(_source.m_Height), m_Visible(_source.m_Visible)
{

}

SDLSprite::~SDLSprite()
{
	SDL_FreeSurface(m_Source);
}

void SDLSprite::SetFrame(int _frameX, int _frameY, int _width, int _height)
{
	m_PosX = 0;
	m_PosY = 0;
	m_FrameX = _frameX;
	m_FrameY = _frameY;
	m_Width = _width;
	m_Height = _height;
}

void SDLSprite::SetFrame(int _posX, int _posY, int _frameX, int _frameY, int _width, int _height)
{
	m_PosX = _posX;
	m_PosY = _posY;
	m_FrameX = _frameX;
	m_FrameY = _frameY;
	m_Width = _width;
	m_Height = _height;
}

void SDLSprite::Draw()
{
	if(m_Visible)
	{
		SDL_Rect frameRect;
		frameRect.x = m_FrameX;
		frameRect.y = m_FrameY;
		frameRect.w = m_Width;
		frameRect.h = m_Height;
		ApplySurface(m_PosX, m_PosY, &frameRect,  m_Source, SDLRenderer::GetInstance()->GetScreen());
	}
}

void SDLSprite::ApplySurface(int _posX, int _posY, SDL_Rect* const _srcRect, SDL_Surface* _src, SDL_Surface* _dest)
{
	SDL_Rect offset;
	offset.x = _posX;
	offset.y = _posY;

	SDL_BlitSurface(_src, _srcRect, _dest, &offset);
}
